package com.yunmang.DaoUtils;

import android.content.Context;

import com.yunmang.entity.DatabaseEntity.TransportDatabase;
import com.yunmang.greendao.gen.TransportDatabaseDao;

import org.greenrobot.greendao.query.QueryBuilder;

import java.util.List;

/**
 * Created by Administrator on 2017/5/16.
 */
public class TransportManager extends BaseDao<TransportDatabase>{
    public TransportManager(Context context) {
        super(context);
    }

    /**
     * 根据下一站获取所有数据
     * @param next
     * @return
     */
    public List<TransportDatabase> getAllByNextStation(String next){
        QueryBuilder queryBuilder =  daoSession.getTransportDatabaseDao().queryBuilder();
        queryBuilder.where(TransportDatabaseDao.Properties.Next.eq(next));
        int size = queryBuilder.list().size();
        if (size > 0){
            return queryBuilder.list();
        }else{
            return null;
        }
    }

    /***
     * 查询数据库查看是否存在该hdbh
     * @param hdbh
     * @return
     */
    public boolean isExitThisData(String hdbh){
        QueryBuilder queryBuilder =  daoSession.getTransportDatabaseDao().queryBuilder();
        queryBuilder.where(TransportDatabaseDao.Properties.Hdbh.eq(hdbh));
        int size = queryBuilder.list().size();
        if (size > 0){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 根据hdbh删除数据库中的数据
     * @param orderid
     */
    public void deleteDataByid(long orderid){
        daoSession.getTransportDatabaseDao().deleteByKey(orderid);
    }

    /**
     * 根据hdbh查询表，去得到对应的主键id
     * @param hdbh
     */
    public Long getTableIdByHdbh(String hdbh){
        QueryBuilder<TransportDatabase> queryBuilder =  daoSession.getTransportDatabaseDao().queryBuilder();
        queryBuilder.where(TransportDatabaseDao.Properties.Hdbh.eq(hdbh));
        List<TransportDatabase> list = queryBuilder.list();
        if(list.size()>0){
            return list.get(0).getTableid();
        }
        return null;
    }

    /**
     * 根据货单编号修改扫描数
     * @param hdbh
     * @param saomiaonum
     */
    public void updateSaomiaoNum(String hdbh,String saomiaonum){
//        daoSession.getTransportDatabaseDao().update();
    }

    /**
     * 是否上传
     * @param id
     * @return
     */
    public boolean getIsUpload(Long id){
        boolean flag=false;
        TransportDatabase transportDatabase = QueryById(id, TransportDatabase.class);
        String saomiao=transportDatabase.getSaomiaonum().trim();
        String num=transportDatabase.getNumbers().trim();
        if(saomiao.equals(num)){
            flag=true;
        }
        return flag;
    }
}
